import sys
import select
import threading

from lib import log, event

class KeyboardEvent(event.Event): pass

class KeyboardPollerThread(threading.Thread):
	def __init__(self, eventQueue):
		super(KeyboardPollerThread, self).__init__()
		self._eventQueue = eventQueue
		self._stop = threading.Event()
	
	@property
	def eventQueue(self): return self._eventQueue
	
	def stop(self):
		self._stop.set()
		
	def run(self):
		log.info("KeyboardPoller running")
		while not self._stop.is_set():
			i, o, e = select.select([sys.stdin],[],[],0.0001)		
			for s in i:
				if s == sys.stdin:
					command = sys.stdin.readline()
					self.eventQueue.registerEvent(KeyboardEvent('command', command[:-1]))
		log.info("KeyboardPoller exiting")